-- UIItemDesc
-- Create by zouyb
-- 仓库道具详细界面

--UIItemDesc继承自Layer
UIItemDesc = class("UIItemDesc", function()
    return cc.Layer:create();
end);

function UIItemDesc.create(itemId, sellItem, para, showAmount)
    return UIItemDesc.new(itemId, sellItem, para, showAmount);
end

-- 背景框切片数量
local SMALL_BG_COUNT = 4;

-- 构造函数
function UIItemDesc:ctor(itemId, sellItem, para, showAmount)
    self:setName("UIItemDesc");
    self.itemId = itemId;

    -- 初始化
    self:setLocalZOrder(UIMgr.BONUS_NOTIFY_ZORDER + 1);
    local node = cc.CSLoader:createNode("layout/workshop/ItemDesc.csb");
    self:addChild(node);
    self.node = node;

    self.sellItem = sellItem;
    self.para = para or {};
    self.showAmount = showAmount;

    -- 注册点击事件
    self:registerTouchEvent();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();
end

-- 注册点击事件
function UIItemDesc:registerTouchEvent()
    -- 注册返回按钮点击事件
    local btnBack = findChildByName(self.node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            AudioM.playFx("button_return");
            UIMgr.getCurrentScene():removeFormByName("UIItemDesc");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    -- 注册出售按钮事件
    local btnSell = findChildByName(self.node, "CT/btn_sell");
    local function onSellClick(sender, eventType)

        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_click");

            self:showSellConfirm(self.itemId);
        end
    end

    btnSell:addTouchEventListener(onSellClick);
    TextStyleM.setTextStyle(btnSell, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnSell:setTitleText(getLocStr("btn_text_sell2"));

    -- 注册背景点击事件
    local bgImg = findChildByName(self.node, "bg/bgImg");
    local function onBgClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            AudioM.playFx("button_return");
            UIMgr.getCurrentScene():removeFormByName("UIItemDesc");
        end
    end
    bgImg:addTouchEventListener(onBgClick);

    -- 防止点击事件被吞掉
    local bg = findChildByName(self.node, "CT/bg");
    for i = 1, SMALL_BG_COUNT do
        local subBg = findChildByName(bg, "bg" .. i);
        if subBg ~= nil then
            subBg:setSwallowTouches(false);
        end
    end
end

-- 绘制属性
function UIItemDesc:redrawAttrib()
    local attrib = self.itemId;
    local name   = FieldsM.query(attrib, "name");
    local desc   = FieldsM.query(attrib, "desc");
    local detail = FieldsM.query(attrib, "detail");
    if self.para and self.para["desc"] then
        desc = self.para["desc"];
    end
    if self.para and self.para["detail"] then
        detail = self.para["detail"];
    end

    -- 属性名称
    local nameLabel = findChildByName(self.node, "CT/name");
    TextStyleM.setTextStyle(nameLabel,TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BLUE);
    nameLabel:setString(name);

    -- 属性描述
    local descLabel = findChildByName(self.node, "CT/desc");
    TextStyleM.setTextStyle(descLabel,TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);
    descLabel = applyString(descLabel, desc);

    local list = { nameLabel, descLabel };
    -- 属性细节
    local detailLabel = findChildByName(self.node, "CT/detail");
    TextStyleM.setTextStyle(detailLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);
    if type(detail) == "string" then
        if FormulaM.invoke("NEED_SHOW_FIELDS_NUM", attrib) then
            -- 描述里需要动态显示数量的
            detail = string.format(detail, ME.user:queryAttrib(attrib));
        end

        detail = "[blue]" .. detail .. "[-]";
        detailLabel = applyString(detailLabel, detail);

        table.insert(list, detailLabel);
    end

    -- 额外信息
    local extraLabel = findChildByName(self.node, "CT/extra");
    TextStyleM.setTextStyle(extraLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);
    if self.para and self.para["extra"] and type(self.para["extra"]) == "string" then
        local extra = "[green]" .. self.para["extra"] .. "[-]";
        extraLabel = applyString(extraLabel, extra);

        table.insert(list, extraLabel);
    end

    -- 自适应所有组件
    local bg = findChildByName(self.node, "CT/bg");
    AlignM.adaptLayout(bg, AlignM.BG_SIZE_SMALL, list);

    -- 按钮隐藏
    local btnSell = findChildByName(self.node, "CT/btn_sell");
    btnSell:setVisible(false);
end

-- 重绘
function UIItemDesc:redraw()
    if type(self.itemId) == "string" then
        -- 属性描述
        self:redrawAttrib();
        return;
    end

    -- 星级
    local itemId = self.itemId;
    local rank = ItemM.query(itemId, "rank");
    local starNode = findChildByName(self.node, "CT/star_node");
    local desc   = self.para["desc"] or ItemM.getItemDesc(itemId);
    local detail = self.para["detail"] or ItemM.getItemDetail(itemId);

    -- 显示星星
    local needShow = FormulaM.invoke("NEED_SHOW_STAR", itemId);
    if needShow then
        local starLayer = createStarLayer(rank, "images/ui/symbol/star_large.png", "right");
        if starLayer ~= nil then
            starNode:addChild(starLayer);
        end
    end

    -- 道具名称
    local nameLabel = findChildByName(self.node, "CT/name");
    TextStyleM.setTextStyle(nameLabel,TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BLUE);
    if self.para and self.para["name"] then
        -- 有参数修正
        nameLabel:setString(self.para["name"]);
    else
        nameLabel:setString(ItemM.query(itemId, "name"));
    end

    -- 道具描述
    local descLabel = findChildByName(self.node, "CT/desc");
    TextStyleM.setTextStyle(descLabel,TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);
    descLabel = applyString(descLabel, desc);

    local list =  {nameLabel, descLabel};

    local group = ItemM.query(itemId, "group");

    local needDetail = {1, 12, 13, 25, 16, 28, 27, 29, 30, 32, 37};

    local detailLabel = findChildByName(self.node, "CT/detail");
    if table.indexOf(needDetail, group) ~= -1 or self.para["id"] then
        -- 道具详细信息
        if table.indexOf({1, 25, 27, 28, 29, 30, 32, 37}, group) ~= -1 or self.para["id"] then
            -- 本身不是蓝色，需要适应
            detail = "[blue]" .. detail .. "[-]";
        end

        TextStyleM.setTextStyle(detailLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);
        detailLabel = applyString(detailLabel, detail);

        table.insert(list, detailLabel);
    end

    local extraLabel = findChildByName(self.node, "CT/extra");
    if self.showAmount then  -- 额外信息
        extraLabel:setVisible(true);
        TextStyleM.setTextStyle(extraLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_GREEN);
        local amount = ItemM.getAmount(ME.user, itemId);
        extraLabel:setString(string.format(getLocStr("own_goods_amount3"), amount));

        table.insert(list, extraLabel);
    end

    -- 如果是符石，则描述里面要加一些内容
    local stoneStyle = SkyStoneM.query(itemId, "style");
    if stoneStyle then
        extraLabel:setVisible(true);
        TextStyleM.setTextStyle(extraLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_BLUE,
                                nil, nil, nil, nil, nil, TextStyleM.TEXT_SIZE_SMALL);
        local extra = string.format(getLocStr("stone_detail"), getLocStr("stone_style_" .. stoneStyle));
        extraLabel = applyString(extraLabel, extra);
        table.insert(list, extraLabel);
    end

    local btnSell;
    if true == self.sellItem then
        btnSell = findChildByName(self.node, "CT/btn_sell");
        btnSell:setVisible(true);

        table.insert(list, btnSell);
    end

    -- 自适应所有组件
    local bg = findChildByName(self.node, "CT/bg");
    AlignM.adaptLayout(bg, AlignM.BG_SIZE_SMALL, list);

    -- 星级节点的纵坐标要与名称保持一致
    starNode:setPositionY(nameLabel:getPositionY() + 5);
end

-- 适配
function UIItemDesc:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

function UIItemDesc:showSellConfirm(classId)
    local ownAmount = ItemM.getAmount2(ME.user, classId);

    -- 出售全部
    local sellPrice = FormulaM.invoke("CALC_SELL_REAGENT_PRICE", classId);

    -- 只显示金币价格
    local price;
    for k, v in pairs(sellPrice) do
        if "money" == v[2] then
            price = v;
        end
    end

    local sellAll = function()
        if ItemM.sellItem(classId, sellPrice, ownAmount) then
            Operation.cmd_sell_item(classId, ownAmount);
        end

        -- 关闭当前界面
        UIMgr.getCurrentScene():removeFormByName("UIItemDesc");
    end

    -- 出售1个
    local sellOne = function()
        if ItemM.sellItem(classId, sellPrice, 1) then

            Operation.cmd_sell_item(classId, 1);
        end

        -- 出售完了关闭界面
        if ItemM.getAmount2(ME.user, classId) < 1 then
            UIMgr.getCurrentScene():removeFormByName("UIItemDesc");
        end
    end

    local title = getLocStr("title_sell_reagent");
    local itemName = ItemM.query(classId, "name");

    if ownAmount == 1 then
        -- 只有一个
        local msg = string.format(getLocStr("sell_item_confirm"), itemName, price[3]);
        local uiConfirm = confirm2(CONFIRM_TYPE_OK, title, msg, sellOne);
    else
        -- 有多个
        local msg = string.format(getLocStr("sell_item_confirm2"), itemName, price[3],
            price[3] * ownAmount);
        local sellAllText = string.format(getLocStr("btn_text_sell"), ownAmount);
        local sellOneText = string.format(getLocStr("btn_text_sell"), 1);

        if getLang() ~= "zh" and getLang() ~= "tw" then
            sellAllText = getLocStr("btn_text_all");
            sellOneText = getLocStr("btn_text_one");
        end

        local uiConfirm = confirm2(CONFIRM_TYPE_OK_CANCEL, title, msg,
            sellAll,
            sellOne,
            sellAllText,
            sellOneText);
    end
end
